Python Testing with pytest by Brian Okken
Author:Brian Okken
Language: eng
Format: epub, pdf
Tags: Pragmatic Bookshelf
Publisher: Pragmatic Bookshelf
Using capsys
The capsys builtin fixture provides two bits of functionality: it allows you to retrieve stdout and stderr from some code, and it disables output capture temporarily. Let’s take a look at retrieving stdout and stderr.
Suppose you have a function to print a greeting to stdout:
ch4/cap/test_capsys.py
def greeting(name):
print('Hi, {}'.format(name))
You can’t test it by checking the return value. You have to test stdout somehow. You can test the output by using capsys:
ch4/cap/test_capsys.py
def test_greeting(capsys):
greeting('Earthling')
out, err = capsys.readouterr()
assert out == 'Hi, Earthling\n'
assert err == ''
greeting('Brian')
greeting('Nerd')
out, err = capsys.readouterr()
assert out == 'Hi, Brian\nHi, Nerd\n'
assert err == ''
The captured stdout and stderr are retrieved from capsys.redouterr(). The return value is whatever has been captured since the beginning of the function, or from the last time it was called.
The previous example only used stdout. Let’s look at an example using stderr:
ch4/cap/test_capsys.py
def yikes(problem):
print('YIKES! {}'.format(problem), file=sys.stderr)
def test_yikes(capsys):
yikes('Out of coffee!')
out, err = capsys.readouterr()
assert out == ''
assert 'Out of coffee!' in err
pytest usually captures the output from your tests and the code under test. This includes print statements. The captured output is displayed for failing tests only after the full test session is complete. The -s option turns off this feature, and output is sent to stdout while the tests are running. Usually this works great, as it’s the output from the failed tests you need to see in order to debug the failures. However, you may want to allow some output to make it through the default pytest output capture, to print some things without printing everything. You can do this with capsys. You can use capsys.disabled() to temporarily let output get past the capture mechanism.
Here’s an example:
ch4/cap/test_capsys.py
def test_capsys_disabled(capsys):
with capsys.disabled():
print('\nalways print this')
print('normal print, usually captured')
Now, ’always print this’ will always be output:
$ cd /path/to/code/ch4/cap
$ pytest -q test_capsys.py::test_capsys_disabled
always print this
.
1 passed in 0.01 seconds
$ pytest -q -s test_capsys.py::test_capsys_disabled
always print this
normal print, usually captured
.
1 passed in 0.00 seconds
As you can see, always print this shows up with or without output capturing, since it’s being printed from within a with capsys.disabled() block. The other print statement is just a normal print statement, so normal print, usually captured is only seen in the output when we pass in the -s flag, which is a shortcut for --capture=no, turning off output capture.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Hello! Python by Anthony Briggs(9914)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9795)
The Mikado Method by Ola Ellnestam Daniel Brolund(9777)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8296)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7778)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Windows APT Warfare by Sheng-Hao Ma(6825)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6555)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6423)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6413)
Kotlin in Action by Dmitry Jemerov(5062)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4316)
Functional Programming in JavaScript by Mantyla Dan(4038)
Solidity Programming Essentials by Ritesh Modi(3995)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3786)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3730)
The Ultimate iOS Interview Playbook by Avi Tsadok(3705)
